package cn.itcast.tanhua.mytanhuasso.controller;

import cn.itcast.tanhua.constant.MessageConstant;
import cn.itcast.tanhua.mytanhuasso.service.UserInfoService;
import cn.itcast.tanhua.mytanhuasso.service.UserService;
import cn.itcast.tanhua.vo.ErrorResult;
import cn.itcast.tanhua.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * UserController
 * @author 郭文亮
 * @since 2021/11/22 19:27
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 生成短信验证码发给mobile
     * @param param 主要用来接收phone
     * @return 成功返回状态码200，失败返回对应的ErrorResult
     */
    @PostMapping("login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String,String> param){
        String mobile = param.get("phone");
        ErrorResult errorResult = null;
        try {
            errorResult = this.userService.sendCheckCode(mobile);
            if (null == errorResult ) {
                // 没有error直接返回状态码200
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("发送短信验证码失败~ mobile = " + mobile , e);
            // 返回发送失败
            ErrorResult.builder()
                    .errCode(MessageConstant.CHECK_CODE_SEND_ERROR_CODE)
                    .errMessage(MessageConstant.CHECK_CODE_SEND_ERROR)
                    .build();
        }
        // 有问题就返回500的状态码，以及errorResult
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 根据用户输入的验证码进行登录
     * @param param 手机号和用户输入的验证码
     * @return 返回用户登录的结果
     */
    @PostMapping("loginVerification")
    public ResponseEntity<Object> login(@RequestBody Map<String,String> param){
        String mobile = param.get("phone");
        String verificationCode = param.get("verificationCode");
        Map<String,Object> map =  this.userService.login(mobile,verificationCode);
        if (null != map  ){
            // 返回验证成功
            return ResponseEntity.ok(map);
        }
        // 返回验证失败
        ErrorResult errorResult = ErrorResult.builder()
                .errCode(MessageConstant.LOGIN_VERIFY_ERROR_CODE)
                .errMessage(MessageConstant.LOGIN_VERIFY_ERROR)
                .build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 存储用户信息
     * @param param 用户信息，包含用户昵称、性别、所在城市、生日
     * @param token token，主要存取用户id
     * @return 添加成功返回200状态码，添加失败返回失败信息errorResult
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody Map<String,String> param , @RequestHeader("Authorization") String token){
        Boolean insertResult =  this.userInfoService.saveUserInfo(param,token);
        if (insertResult) {
            // 添加成功
            return ResponseEntity.ok(null);
        }
        // 返回添加失败
        ErrorResult errorResult = ErrorResult.builder()
                .errCode(MessageConstant.INSERT_USERINFO_ERROR_CODE)
                .errMessage(MessageConstant.INSERT_USERINFO_ERROR)
                .build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 校验token，根据token查询user信息
     * @param token token信息
     * @return user，主要是返回user的mobile
     */
    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token) {
        return this.userService.queryUserByToken(token);
    }

    /**
     * 上传头像
     * @param multipartFile 头像图片
     * @param token token信息
     * @return 上传成功返回状态码200，失败返回errorResult
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> loginReginfoLogo(@RequestParam("headPhoto") MultipartFile multipartFile, @RequestHeader("Authorization") String token) {
        Boolean updateResult = this.userInfoService.updateUserInfoLogo(multipartFile,token);
        if (updateResult) {
            // 头像上传成功
            return ResponseEntity.ok(null);
        }
        // 返回上传失败
        ErrorResult errorResult = ErrorResult.builder()
                .errCode(MessageConstant.UPDATE_USERINFO_LOGO_ERROR_CODE)
                .errMessage(MessageConstant.UPDATE_USERINFO_LOGO_ERROR)
                .build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);

    }

}
